﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI.MobileControls;
using TransFormer.Model;
using ProductType = TransFormer.BLL.ProductType;

namespace TransFormer.Web.ashx
{
    /// <summary>
    /// GetProductTypes 的摘要说明
    /// </summary>
    public class GetProductTypes : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var bll = new ProductType();
            var result = bll.GetModelList(string.Empty);
            var jz = new JavaScriptSerializer();
            var result2 = from productType in result
                          select new ProductTypeForTree()
                                     {
                                         id = productType.Id,
                                         pId = productType.ParrentId,
                                         name = productType.TypeName,
                                         url = "Product.aspx?productTypeId=" + productType.Id,
                                         target = "_self",
                                     };
            var productTypeForTrees = result2 as List<ProductTypeForTree> ?? result2.ToList();
            foreach (var productTypeForTree in productTypeForTrees)
            {
                if (productTypeForTrees.Any(x => productTypeForTree.id == x.pId))
                    productTypeForTree.url = "javascript:void(0);";

            }
            context.Response.Write(jz.Serialize(productTypeForTrees));

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}